package com.green.common.page.grid.utils;

import java.io.Serializable;

/**
 * 
 * @author lineshow email:lineshow7@gmail.com
 * @version 1.0
 * 
 *          PageContainer.java created on 2011-7-16
 * 
 *          description: 分页查询容器
 */
public class PageContainer implements Serializable {

	private static final long serialVersionUID = 6234939577965141720L;
	private Integer currentPage = new Integer(1); // 当前页号
	private Integer totalPages = new Integer(1); // 总页数
	private Integer totalCount = new Integer(0);// 总行数
	private Integer pageRowCount; // 页显示行
	private Grid grid; // 显示数据

	public PageContainer() {
		grid = new Grid();
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getPageRowCount() {
		return pageRowCount;
	}

	public void setPageRowCount(Integer pageRowCount) {
		this.pageRowCount = pageRowCount;
	}

	public Grid getGrid() {
		return grid;
	}

	public void setGrid(Grid grid) {
		this.grid = grid;
	}

	public void setTotalPages() {
		if (this.pageRowCount != 0) {
			this.totalPages = this.totalCount / this.pageRowCount + (this.totalCount % this.pageRowCount == 0 ? 0 : 1);
		}
	}

	private void add2TotalCount(Integer subTotalCount) {
		totalCount += subTotalCount;
	}

	private void add2Grid(Grid gridParam) {
		this.grid.merge(gridParam);
	}

	/**
	 * merge multi grid-containers into one.
	 * 
	 * @author lineshow created on 2011-12-3
	 * @param pageContainers
	 * @return
	 */
	public static PageContainer merge(PageContainer... pageContainers) {
		if (pageContainers == null) {
			return null;
		}
		PageContainer includeContainer = new PageContainer();
		for (PageContainer pcer : pageContainers) {
			includeContainer.add2TotalCount(pcer.getTotalCount());
			includeContainer.add2Grid(pcer.getGrid());
		}
		return includeContainer;
	}

}
